package cn.ibizlab.odoo.core.odoo_product.service.impl;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.math.BigInteger;
import java.util.Collection;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import cn.ibizlab.odoo.core.client.model.Iproduct_attribute_custom_value;
import cn.ibizlab.odoo.core.odoo_product.domain.Product_attribute_custom_value;
import cn.ibizlab.odoo.core.odoo_product.filter.Product_attribute_custom_valueSearchContext;
import cn.ibizlab.odoo.core.odoo_product.service.IProduct_attribute_custom_valueService;
import cn.ibizlab.odoo.util.SearchResult;
import cn.ibizlab.odoo.util.log.IBIZLog;

/**
 * 实体[产品属性自定义值] 服务对象接口实现
 */
@Slf4j
@IBIZLog
@Service
public class Product_attribute_custom_valueServiceImpl implements IProduct_attribute_custom_valueService {

    @Autowired
    private cn.ibizlab.odoo.core.client.service.Iproduct_attribute_custom_valueClientService product_attribute_custom_valueClientService;

    @Override
    public void removeBatch(Collection<? extends Serializable> idList, int batchSize) {

        //product_attribute_custom_valueClientService.removeBatch(product_attribute_custom_values) ;

    }

    @Override
    public boolean create(Product_attribute_custom_value product_attribute_custom_value) {
        boolean bOk = false ;
        Iproduct_attribute_custom_value clientModel = this.convert2Model(product_attribute_custom_value,null);
        product_attribute_custom_valueClientService.create(clientModel) ;
        this.convert2Domain(clientModel,product_attribute_custom_value);
        bOk = true ;

        return bOk ;
    }

    @Override
    public boolean remove(Integer id) {
        boolean bOk = false ;
        Iproduct_attribute_custom_value clientModel = product_attribute_custom_valueClientService.createModel();
        clientModel.setId(id);
        product_attribute_custom_valueClientService.remove(clientModel) ;
        bOk = true ;
        return bOk ;
    }

    @Override
    public boolean update(Product_attribute_custom_value product_attribute_custom_value) {
        boolean bOk = false ;
        Iproduct_attribute_custom_value clientModel = this.convert2Model(product_attribute_custom_value,null);
        product_attribute_custom_valueClientService.update(clientModel) ;
        this.convert2Domain(clientModel,product_attribute_custom_value) ;
        bOk = true;
        return bOk ;
    }

    @Override
    public void updateBatch(List<Product_attribute_custom_value> product_attribute_custom_values, int batchSize) {

        //product_attribute_custom_valueClientService.updateBatch(product_attribute_custom_values,batchSize) ;

    }

    @Override
    public Product_attribute_custom_value get(Integer id) {
        Iproduct_attribute_custom_value clientModel = product_attribute_custom_valueClientService.createModel();
        clientModel.setId(id);
        product_attribute_custom_valueClientService.get(clientModel) ;
        return this.convert2Domain(clientModel,null);
    }

    @Override
    public Product_attribute_custom_value get(Product_attribute_custom_value product_attribute_custom_value) {
        Product_attribute_custom_value retproduct_attribute_custom_value = this.get(product_attribute_custom_value.getId()) ;
    	BeanCopier copier=BeanCopier.create(retproduct_attribute_custom_value.getClass(), product_attribute_custom_value.getClass(), false);
        copier.copy(retproduct_attribute_custom_value, product_attribute_custom_value, null);
        return product_attribute_custom_value ;
    }

    @Override
    public void createBatch(List<Product_attribute_custom_value> product_attribute_custom_values, int batchSize) {

                        //product_attribute_custom_valueClientService.createBatch(product_attribute_custom_values,batchSize) ;

    }

    /**
     * 查询集合 默认查询
     */
    @Override
    public Page<Product_attribute_custom_value> searchDefault(Product_attribute_custom_valueSearchContext context) {
        List<Product_attribute_custom_value> list = new ArrayList<Product_attribute_custom_value>();
        Page<cn.ibizlab.odoo.core.client.model.Iproduct_attribute_custom_value> clientModelList = product_attribute_custom_valueClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Iproduct_attribute_custom_value clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Product_attribute_custom_value>(list, context.getPageable(), clientModelList.getTotalElements());
    }


    /**
     * 根据属性值查询
     */
    @Override
    public Page<Product_attribute_custom_value> selectByOdoo_attribute_value(Integer id){
        Product_attribute_custom_valueSearchContext context = new Product_attribute_custom_valueSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Product_attribute_custom_value> list = new ArrayList<Product_attribute_custom_value>();
        Page<cn.ibizlab.odoo.core.client.model.Iproduct_attribute_custom_value> clientModelList = product_attribute_custom_valueClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Iproduct_attribute_custom_value clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Product_attribute_custom_value>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Product_attribute_custom_value> selectByOdoo_create(Integer id){
        Product_attribute_custom_valueSearchContext context = new Product_attribute_custom_valueSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Product_attribute_custom_value> list = new ArrayList<Product_attribute_custom_value>();
        Page<cn.ibizlab.odoo.core.client.model.Iproduct_attribute_custom_value> clientModelList = product_attribute_custom_valueClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Iproduct_attribute_custom_value clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Product_attribute_custom_value>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Product_attribute_custom_value> selectByOdoo_write(Integer id){
        Product_attribute_custom_valueSearchContext context = new Product_attribute_custom_valueSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Product_attribute_custom_value> list = new ArrayList<Product_attribute_custom_value>();
        Page<cn.ibizlab.odoo.core.client.model.Iproduct_attribute_custom_value> clientModelList = product_attribute_custom_valueClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Iproduct_attribute_custom_value clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Product_attribute_custom_value>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据销售订单行查询
     */
    @Override
    public Page<Product_attribute_custom_value> selectByOdoo_sale_order_line(Integer id){
        Product_attribute_custom_valueSearchContext context = new Product_attribute_custom_valueSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Product_attribute_custom_value> list = new ArrayList<Product_attribute_custom_value>();
        Page<cn.ibizlab.odoo.core.client.model.Iproduct_attribute_custom_value> clientModelList = product_attribute_custom_valueClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Iproduct_attribute_custom_value clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Product_attribute_custom_value>(list, context.getPageable(), clientModelList.getTotalElements());

    }



    public Iproduct_attribute_custom_value convert2Model(Product_attribute_custom_value domain , Iproduct_attribute_custom_value model) {
        if(domain == null)
            return model ;
        if(model==null){
            model = product_attribute_custom_valueClientService.createModel() ;
        }

         if(domain.getCreate_dateDirtyFlag())
            model.setCreate_date(domain.getCreate_date());
         if(domain.get__last_updateDirtyFlag())
            model.set__last_update(domain.get__last_update());
         if(domain.getDisplay_nameDirtyFlag())
            model.setDisplay_name(domain.getDisplay_name());
         if(domain.getIdDirtyFlag())
            model.setId(domain.getId());
         if(domain.getCustom_valueDirtyFlag())
            model.setCustom_value(domain.getCustom_value());
         if(domain.getWrite_dateDirtyFlag())
            model.setWrite_date(domain.getWrite_date());
         if(domain.getWrite_uid_textDirtyFlag())
            model.setWrite_uid_text(domain.getWrite_uid_text());
         if(domain.getCreate_uid_textDirtyFlag())
            model.setCreate_uid_text(domain.getCreate_uid_text());
         if(domain.getSale_order_line_id_textDirtyFlag())
            model.setSale_order_line_id_text(domain.getSale_order_line_id_text());
         if(domain.getAttribute_value_id_textDirtyFlag())
            model.setAttribute_value_id_text(domain.getAttribute_value_id_text());
         if(domain.getCreate_uidDirtyFlag())
            model.setCreate_uid(domain.getCreate_uid());
         if(domain.getWrite_uidDirtyFlag())
            model.setWrite_uid(domain.getWrite_uid());
         if(domain.getSale_order_line_idDirtyFlag())
            model.setSale_order_line_id(domain.getSale_order_line_id());
         if(domain.getAttribute_value_idDirtyFlag())
            model.setAttribute_value_id(domain.getAttribute_value_id());
        return model ;
    }

    public Product_attribute_custom_value convert2Domain( Iproduct_attribute_custom_value model ,Product_attribute_custom_value domain) {
        if(model == null)
            return domain ;
        if(domain == null){
            domain = new Product_attribute_custom_value();
        }

        if(model.getCreate_dateDirtyFlag())
            domain.setCreate_date(model.getCreate_date());
        if(model.get__last_updateDirtyFlag())
            domain.set__last_update(model.get__last_update());
        if(model.getDisplay_nameDirtyFlag())
            domain.setDisplay_name(model.getDisplay_name());
        if(model.getIdDirtyFlag())
            domain.setId(model.getId());
        if(model.getCustom_valueDirtyFlag())
            domain.setCustom_value(model.getCustom_value());
        if(model.getWrite_dateDirtyFlag())
            domain.setWrite_date(model.getWrite_date());
        if(model.getWrite_uid_textDirtyFlag())
            domain.setWrite_uid_text(model.getWrite_uid_text());
        if(model.getCreate_uid_textDirtyFlag())
            domain.setCreate_uid_text(model.getCreate_uid_text());
        if(model.getSale_order_line_id_textDirtyFlag())
            domain.setSale_order_line_id_text(model.getSale_order_line_id_text());
        if(model.getAttribute_value_id_textDirtyFlag())
            domain.setAttribute_value_id_text(model.getAttribute_value_id_text());
        if(model.getCreate_uidDirtyFlag())
            domain.setCreate_uid(model.getCreate_uid());
        if(model.getWrite_uidDirtyFlag())
            domain.setWrite_uid(model.getWrite_uid());
        if(model.getSale_order_line_idDirtyFlag())
            domain.setSale_order_line_id(model.getSale_order_line_id());
        if(model.getAttribute_value_idDirtyFlag())
            domain.setAttribute_value_id(model.getAttribute_value_id());
        return domain ;
    }


}
